package com.fifth.user.config;
 
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.UUID;

/*
* 自动填充配置类*/
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    //当添加时自动填充的值
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        //this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)
        //  切记这里是表的列名  不是实体类的属性名
        //自动生成创建时间
        this.strictInsertFill(metaObject, "createtime", () -> LocalDateTime.now(), LocalDateTime.class); // 起始版本 3.3.3(推荐)
        //默认正常状态
        this.strictInsertFill(metaObject, "status", () -> 0, Integer.class);
        //
        this.strictInsertFill(metaObject, "state", () -> 0, Integer.class);
        //逻辑删除列
        //this.strictInsertFill(metaObject, "is_deleted", () -> 0, Integer.class);
        // 或者
        //this.fillStrategy(metaObject, "createTime", LocalDateTime.now()); // 也可以使用(3.3.0 该方法有bug)
    }
    //当修改时自动填充的值
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
        this.strictUpdateFill(metaObject, "updatetime",()-> LocalDateTime.now(),LocalDateTime.class); // 起始版本 3.3.0(推荐)
    }
}